package com.example.fruitpool;

import java.util.Random;

import org.andengine.engine.camera.Camera;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.entity.sprite.Sprite;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.TextureRegion;
import org.andengine.opengl.util.GLState;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

import android.util.Log;

import com.example.manager.ResourcesManager;

public class FruitPool{

	private static FruitPool INSTANCE = new FruitPool();
	public static ResourcesManager resourcesManager;
	public static VertexBufferObjectManager vbom;
	
	Random ran = new Random();
	
	public static void prepareManager(ResourcesManager presourcesManager, VertexBufferObjectManager pvbom) 
	{
		resourcesManager = presourcesManager; 
		vbom = pvbom;
		//Log.d("score", mArrFruit.size()+"fruit");
	}    
	public static FruitPool sharedFruitPool()
	{
		return INSTANCE;
	} 
	private FruitPool() {
		super();
	}

	public Fruit onAllocatePoolItem() {

		int i = ran.nextInt(resourcesManager.mArrFruit.size());
		AnimatedSprite s;
		s = new AnimatedSprite(0, 0, resourcesManager.mArrFruit.get(i), vbom) {
			@Override
			protected void preDraw(final GLState pGLState, final Camera pCamera) {
				super.preDraw(pGLState, pCamera);
				pGLState.enableDither();
			}
		};

		s.animate(50, true);

		// Sprite s = new Sprite(0, 0, ball, vbom);
		return new Fruit(s, resourcesManager.mArrFruitId.get(i));

	}
	public Fruit onAllocatePoolItem(String string) {
		int i = 0;
		if(string.equalsIgnoreCase("A")){
			i = 0;
		}else{ //(string.equalsIgnoreCase("B")){
			i = 1;
		}
		AnimatedSprite s;
		s = new AnimatedSprite(0, 0, resourcesManager.mArrFruit.get(i), vbom) {
			@Override
			protected void preDraw(final GLState pGLState, final Camera pCamera) {
				super.preDraw(pGLState, pCamera);
				pGLState.enableDither();
			}
		};

		s.animate(100, true);

		// Sprite s = new Sprite(0, 0, ball, vbom);
		return new Fruit(s, resourcesManager.mArrFruitId.get(i));
	}
	public Fruit onAllocatePoolItemSpec(ITextureRegion spec,String id) {
		
		Sprite s = new Sprite(0, 0,60,90, spec, vbom);
		return new Fruit(s, id);

	}
	public void onHandleRecycleItem(final Fruit e) {
		//e.sprit.dispose();
		e.sprit.clearEntityModifiers();
		e.sprit.clearUpdateHandlers();
		e.sprit.setVisible(false);	
		e.sprit.detachSelf();
		///e.sprit.reset();
		Log.v("Jimvaders", "EnemyPool onHandleRecycleItem()");
	}
	public void onHandleRecycleItemSpec(final Fruit e) {
		//e.sprit.dispose();
		e.spritspec.clearEntityModifiers();
		e.spritspec.clearUpdateHandlers();
		e.spritspec.setVisible(false);	
		e.spritspec.detachSelf();
		///e.sprit.reset();
		Log.v("Jimvaders", "EnemyPool onHandleRecycleItem()");
	}
}